<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\admin\Adlog;
use App\admin\Logo;
use App\admin\Netsetting;
use App\admin\Kefu;
use Illuminate\Support\Facades\DB;
use App\Http\Middleware;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;

class AboutBackNetController extends BaseController {
	public function __construct() {
		$this->middleware('admindo');
	}

	//关于后台
	public function index() {
		return view('admin.aboutBackNet');
	}

	//后台首页,右边初始化内容
	public function Base() {
		return view('admin.base');
	}

	//访问统计
	public function statistics() {
		return view('admin.statistice');
	}

	//数据备份
	public function dataBackup() {
		return view('admin.dataBackup');
	}

	//网站配置
	public function SiteConfiguration() {
		
		$Logo = new Logo;
		$Logos = $Logo->get();
		
		$net = new Netsetting;
		$nets = $net->where('id',1)->get();
		
		return view('admin.siteconfiguration',['Logo'=>$Logos,'nets'=>$nets]);
	}

	public function SiteConfigurationUp(Request $request) {
		$netsite = $request->input('netsite');
		$netname = $request->input('netname');
		$netkw = $request->input('netkw');
		$nettel = $request->input('nettel');
		$netfax = $request->input('netfax');
		
		$netaddr = $request->input('netaddr');
		$netlat = $request->input('netlat');
		$netlnt = $request->input('netlnt');
		$netemial = $request->input('netemial');
		
		$netvideo = $request->input('netvideo');
		$netsetitle = $request->input('netsetitle');
		$netrecord = $request->input('netrecord');

		$net = new Netsetting;
		$flight = $net::find(1);
		$flight->webdomain = $netsite;
		$flight->webname = $netname;
		$flight->webkeywork = $netkw;
		$flight->tel_ = $nettel;
		$flight->fax_ = $netfax;
		
		$flight->addr = $netaddr;
		$flight->lat = $netlat;
		$flight->lnt = $netlnt;
		$flight->emial_ = $netemial;
		
		$flight->videos = $netvideo;
		$flight->secondtitle = $netsetitle;
		$flight->webrecord = $netrecord;
		
				
		if ($request -> hasFile('fileLogo')) {
			$file = $request -> file('fileLogo');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/logo/'. $name;
			Image::make($file) -> save($path);
//			Image::make($file) -> resize(261, 98) -> save($path);
			//保存到数据库
//			$temp = new Logo;
//			$temp->where('id', '1');
			$pathBase = 'upload/logo/'. $name;
//	        $temp->update(['name' => $name,'imgurl'=>$pathBase]);
			DB::table('logos')->where('id', 1)->update(['name' => '公司Logo','imgurl'=>$pathBase]);
		}
		if ($request -> hasFile('fileNetCode')) {
			$file = $request -> file('fileNetCode');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/logo/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/logo/'. $name;
			DB::table('logos')->where('id', 2)->update(['name' => '公司网站二维码','imgurl'=>$pathBase]);
		}
		if ($request -> hasFile('fileRqCode')) {
			$file = $request -> file('fileRqCode');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/logo/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/logo/'. $name;
			DB::table('logos')->where('id', 3)->update(['name' => '公众号二维码','imgurl'=>$pathBase]);
		}
		if($flight->save()){
			//日志增加
			$temp = array();
			$temp['webdomain'] = $netsite;
			$temp['webname'] = $netname;
			$temp['webkeywork'] = $netkw;
			$temp['webrecord'] = $netrecord;
			$str = json_encode($temp);
			$cars=array('name'=>'网站设置上传','info'=>'网站设置上传','cnt'=>$str);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('SiteConfiguration');
		}
		
	}

	//客服设置配置
	public function Kefu() {
		$flight_ = new Kefu;
		$flights = $flight_->where('id', 1)->get();
		return view('admin.kefuh',['data'=>$flights]);
	}
	public function KefuUp(Request $request) {
		$net = new Kefu;
		$flight = $net::find(1);
		$flight->qq = $request->input('qq');
//		$flight->imgurl = $request->input('netsite');
		$flight->wechat = $request->input('wechat');
		$flight->tel = $request->input('tel');
		$flight->mobile = $request->input('mobile');
		
		if ($request -> hasFile('fileqq')) {
			$file = $request -> file('fileqq');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/kefu/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/kefu/'. $name;
			$flight->imgurl = $pathBase;
		}
		if($flight->save()){
			$cars=array('name'=>'客服信息上传','info'=>'客服信息上传','cnt'=>'客服信息上传');
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('kefu');
		}
	}
	//访问日志
	public function AccessLog() {
		$flight = Adlog::orderBy('id', 'desc')->paginate(15);
		return view('admin.accesslog',['data' => $flight]);
	}

}
